/**
 * 
 */
package truerefactor.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

/**
 * Splash - A splash Screen. Code borrowed from:
 * http://www.javaworld.com/javatips/jw-javatip104.html
 * 
 * @author Tony Colston, JavaWorld.com, 11/17/00 Modifications: Isaac Griffith
 */
public class Splash extends JWindow {

    /**
     * Parent window to be displayed when the splash screen is no longer desired
     * to be visible
     */
    private JFrame parent;

    /**
     * Creates a new Splash instance with the provided parent JFrame and
     * specified wait time.
     * 
     * @param f
     *            The parent JFrame of this Splash
     * @param waitTime
     *            the amount of time to display the splash screen prior to
     *            displaying the main parent window. This number is the number
     *            of miliseconds to wait.
     */
    public Splash(JFrame f, int waitTime)
    {
        super(f);
        parent = f;

        JLabel label = new JLabel();
        label.setIcon(new ImageIcon(Splash.class.getResource("/truerefactor/ui/resources/logo2.png")));
        getContentPane().add(label, BorderLayout.CENTER);

        pack();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = label.getPreferredSize();
        setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2));
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e)
            {
                done();
            }
        });

        final int pause = waitTime;
        final Runnable closerRunner = new Runnable() {
            public void run()
            {
                done();
            }
        };

        Runnable waitRunner = new Runnable() {
            public void run()
            {
                try
                {
                    Thread.sleep(pause);
                    SwingUtilities.invokeAndWait(closerRunner);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    // can catch InvocationTargetException
                    // can catch InterruptedException
                }
            }
        };

        setVisible(true);
        Thread splashThread = new Thread(waitRunner, "SplashThread");
        splashThread.start();
    }

    /**
     * Method called when the timer has ended on the display of the splash
     * screen or if the user clicks on it. This sets the splash screen to not be
     * visible and its parent window to be visible.
     */
    private void done()
    {
        setVisible(false);
        parent.setVisible(true);
        dispose();
    }
}
